package com.storm.lib.generator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;


import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import com.storm.lib.generator.bean.Protocal;
import com.storm.lib.util.FileUtil;

public class ProtocalBuilder {


		
	
	/**
	 * 方法功能:构建协议的xml文件
	 * 更新时间:2011-9-15, 作者:johnny
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args){
        try {
			List<Protocal> protocals = new ArrayList<Protocal>();
			File dir = new File(FileUtil.getFileSourcePath()+"\\com\\games\\rpg\\remoting\\");
			File files[] = dir.listFiles();
			int classIndex = 1;
			for (int i = 0; i < files.length; i++) {
				File file2 = files[i];
				if (file2.getName().contains(".svn")) {
					continue;
				}
				int functionIndex = 1;
				BufferedReader br = new BufferedReader(new FileReader(file2));
				String className = null;
				String lineContent = null;
				Protocal currentProtocal = new Protocal();
				while ((lineContent = br.readLine()) != null) {
					if (lineContent.contains("public class")) {
						className = lineContent.split(" ")[2];
					}
					if (className != null) {
						currentProtocal.className = className;
					}
					if (lineContent.contains("方法功能:")) {
						currentProtocal.functionComment = lineContent
								.substring("方法功能:".length()
										+ lineContent.indexOf("方法功能:"));
					}
					if (lineContent.contains("@param")) {
						currentProtocal.paramComment = lineContent
								.substring("@param ".length()
										+ lineContent.indexOf("@param "));
					}
					if (lineContent.contains("@return")) {
						currentProtocal.returnComment = lineContent
								.substring("@return ".length()
										+ lineContent.indexOf("@return "));
					}
					if (lineContent.contains("public ")) {
						if (lineContent.contains("public class")) {
							continue;
						}
						lineContent = lineContent.replaceAll("\\<", "[")
								.replaceAll("\\>", "]");
						String[] vals = lineContent.split(" ");

						currentProtocal.returnName = vals[1];
						String function = lineContent.substring(lineContent
								.indexOf(currentProtocal.returnName)
								+ currentProtocal.returnName.length());
						currentProtocal.functionName = function.substring(1,
								function.indexOf("("));
						currentProtocal.paramName = function.substring(
								1 + function.indexOf("("),
								function.indexOf(")"));
						currentProtocal.id = "" + classIndex + functionIndex;
						functionIndex++;
						String[] funs = currentProtocal.functionName.split(" ");
						if(funs.length>1){
							currentProtocal.functionName=funs[funs.length-1];
						}
						protocals.add(currentProtocal);
						currentProtocal = new Protocal();
					}
				}
				classIndex++;
			}
			XMLWriter writer = null;// 声明写XML的对象
			SAXReader reader = new SAXReader();
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("utf-8");// 设置XML文件的编码格式
			String filePath = "E:\\myspace\\Configure\\WebRoot\\config\\protocal.xml";
//			String filePath = SystemDataCache.protocalPath;
			File file = new File(filePath);
				Document document = DocumentHelper.createDocument();
				Element root = document.addElement("Protocal");

				for (int i = 0; i < protocals.size(); i++) {
					Protocal protocal = protocals.get(i);
					Element command = root.addElement("command");
					command.setAttributeValue("id", protocal.id);
					command.setAttributeValue("name", protocal.functionComment);
					command.setAttributeValue("order1", protocal.className);
					command.setAttributeValue("order2", protocal.functionName);
					Element clientToServer = command
							.addElement("client-to-server");
					clientToServer.addText(protocal.paramName
							+ ("(注释:" + protocal.paramComment + ")"));
					Element serverToClient = command
							.addElement("server-to-client");
					serverToClient.addText(protocal.returnName
							+ ("(注释:" + protocal.returnComment + ")"));
				}
				writer = new XMLWriter(new FileWriter(filePath), format);
				writer.write(document);
				writer.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


